package com.example.waterrecordopengl

import android.annotation.SuppressLint
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import java.util.UUID

class BluetoothActivity : Activity() {


    private var bluetoothAdapter: BluetoothAdapter? = null
    private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            // 处理发现的设备
            val action = intent.getAction()
            if (BluetoothDevice.ACTION_FOUND == action) {
                // 发现设备
                val device = intent.getParcelableExtra<BluetoothDevice?>(BluetoothDevice.EXTRA_DEVICE)
                // 处理发现的设备
                if (device != null) {
                    // 获取设备的名称和地址
                    @SuppressLint("MissingPermission") val name = device.getName()
                    val address = device.getAddress()
                    // 处理发现的设备
                    println("发现设备：" + name + " - " + address)
                    if (address == "EC:21:50:45:79:B0" || name == "IQOO Z7"){
                        Toast.makeText(this@BluetoothActivity, "发现设备：" + name + " - " + address, Toast.LENGTH_SHORT).show()
                        var createBond = device.createBond()
                        println("配对结果，$createBond")

                        var connector = BluetoothConnector(device)
                        connector.connect(this@BluetoothActivity)

                        connector.setData("hello word!")
                    }
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bluetooth)

        findViewById<View?>(R.id.blueTooth).setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                XXPermissions.with(this@BluetoothActivity)
                    .permission(Permission.Group.BLUETOOTH)
                    .request(object : OnPermissionCallback {

                        override fun onGranted(p0: List<String?>, p1: Boolean) {
                            if (p1) {
                                init()
                            }
                        }
                    })

            }
        })
    }

    @SuppressLint("MissingPermission")
    private fun init() {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

        if (bluetoothAdapter == null || !bluetoothAdapter!!.isEnabled()) {
            // 提示用户开启蓝牙
            Toast.makeText(this, "请先开启蓝牙", Toast.LENGTH_SHORT).show()
        } else {
            // 注册广播接收器
            val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
            registerReceiver(receiver, filter)
            bluetoothAdapter!!.startDiscovery()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(receiver)
    }
}
